package com.cskaoyan.service.wx.auth;

import com.cskaoyan.bean.AdminInfoBean;
import com.cskaoyan.bean.BaseRespVo;

import com.cskaoyan.bean.LoginUserData;
import com.cskaoyan.bean.order.Order;
import com.cskaoyan.bean.order.OrderExample;
import com.cskaoyan.bean.wx.auth.WXUser;
import com.cskaoyan.bean.wx.auth.WXUserExample;
import com.cskaoyan.bean.wx.auth.bo.AuthBo;
import com.cskaoyan.bean.wx.auth.vo.OrderVo;
import com.cskaoyan.bean.wx.auth.vo.OrderVo2;
import com.cskaoyan.bean.wx.auth.vo.UserDataVo;
import com.cskaoyan.bean.wx.auth.vo.UserInfo;
import com.cskaoyan.mapper.OrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.mapper.WXUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 创建日期: 2022/05/07 17:07
 *
 * @author JinLu Zhou
 */
@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    WXUserMapper wxUserMapper;
    @Autowired
    OrderMapper orderMapper;

    @Override
    public BaseRespVo login(AuthBo authBo) {
        String username = authBo.getUsername();
        String password = authBo.getPassword();

        Subject subject = SecurityUtils.getSubject();

        subject.login(new UsernamePasswordToken(username, password));

        LoginUserData loginUserData = new LoginUserData();

        AdminInfoBean adminInfo = new AdminInfoBean();
        adminInfo.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        adminInfo.setNickName("admin123");

        loginUserData.setAdminInfo(adminInfo);
        //loginUserData.setToken("c4d988f3-e4c5-4e46-b6ce-13b9e008ea20");
        loginUserData.setToken((String) subject.getSession().getId());

        WXUserExample wxUserExample = new WXUserExample();
        WXUserExample.Criteria criteria = wxUserExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andUsernameEqualTo(authBo.getUsername());
        List<WXUser> wxUsers = wxUserMapper.selectByExample(wxUserExample);
        AdminInfoBean adminInfoBean = new AdminInfoBean();
        //adminInfoBean.setAvatar(wxUsers.get(0).getAvatar());
        //adminInfoBean.setNickName(wxUsers.get(0).getNickname());
        adminInfoBean.setAvatar("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic2.zhimg.com%2Fv2-b3304ca0ecdc6a8a3b13a17e3afcfac5_b.jpg&refer=http%3A%2F%2Fpic2.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654783448&t=26d404cbda10f2202d588f65defc7579");
        // adminInfoBean.setNickName("孫豪俊牛批");
        // by liutao
        adminInfoBean.setNickName("你看我🦅吗");
        loginUserData.setUserInfo(adminInfoBean);
        return BaseRespVo.ok(loginUserData);
    }

    @Override
    public BaseRespVo index(Integer id) {

        WXUserExample wxUserExample = new WXUserExample();
        WXUserExample.Criteria criteria = wxUserExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<WXUser> wxUsers = wxUserMapper.selectByExample(wxUserExample);
        WXUser wxUser = wxUsers.get(0);

        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria1 = orderExample.createCriteria();
        criteria1.andUserIdEqualTo(wxUser.getId());
        criteria.andDeletedEqualTo(false);
        List<Order> orders = orderMapper.selectByExample(orderExample);

        int uncomment = 0;
        int unpaid = 0;
        int unrecv = 0;
        int unship = 0;
        for (Order order : orders) {
            if (order.getOrderStatus() == 101) {
                unpaid++;
            }
            if (order.getOrderStatus() == 201) {
                unship++;
            }
            if (order.getOrderStatus() == 301) {
                unrecv++;
            }
            if (order.getOrderStatus() == 401) {
                uncomment+=order.getComments();
            }
        }
        OrderVo orderVo = new OrderVo();
        orderVo.setUncomment(uncomment);
        orderVo.setUnpaid(unpaid);
        orderVo.setUnrecv(unrecv);
        orderVo.setUnship(unship);

        BaseRespVo<OrderVo2> baseRespVo = new BaseRespVo<>();
        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        baseRespVo.setData(new OrderVo2(orderVo));
        return baseRespVo;
    }
}
